package com.atguigu.rabbitmq.one;

import com.atguigu.rabbitmq.util.RabbitmqUtil;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;

import java.io.IOException;
import java.util.Map;

/**
 * @author codingBee
 * @since 2021/6/22/20:23
 */
public class Consumer {
    //队列名称
    public static final String QUEUE_NAME = "hello";
    //接收消息
    public static void main(String[] args) throws IOException {
        //获取连接
        Connection connection = RabbitmqUtil.getConnection();
        //获取信道
        Channel channel = connection.createChannel();
        /**
         * Start a non-nolocal, non-exclusive consumer, with
         * a server-generated consumerTag. 创建一个消费者
         * Provide access only to <code>basic.deliver</code> and
         * <code>basic.cancel</code> AMQP methods (which is sufficient
         * for most cases). See methods with a {@link com.rabbitmq.client.Consumer} argument
         * to have access to all the application callbacks.
         * @param queue the name of the queue 队列名称
         * @param autoAck true if the server should consider messages
         * acknowledged once delivered; false if the server should expect
         * explicit acknowledgements 是否自动应答
         * @param deliverCallback callback when a message is delivered 消息传递时的回调
         * @param cancelCallback callback when the consumer is cancelled 当消费者被取消的回调
         * @return the consumerTag generated by the server 由服务器生成的消费者标签
         * @throws IOException if an error is encountered
         * @see com.rabbitmq.client.AMQP.Basic.Consume
         * @see com.rabbitmq.client.AMQP.Basic.ConsumeOk
         * @see #basicAck
         * @see #basicConsume(String, boolean, String, boolean, boolean, Map, com.rabbitmq.client.Consumer)
         * @since 5.0
         */
        channel.basicConsume(QUEUE_NAME,Boolean.TRUE,(consumerTag, message) -> {
            System.out.println(String.format("消息被接收 consumerTag:[%s]\tmessage:[%s]",consumerTag,new String(message.getBody())));
        },(consumerTag)->{
            System.out.println(String.format("消息被中断 consumerTag:[%s]",consumerTag));
        });
    }
}
